var itemCount = 10;

var first       = ' <a class="first" href="{url}">第一页</a>';
var last        = ' <a class="last" href="{url}">最后一页</a>';
var prev        = ' <a class="prev" href="{url}">上一页</a>';
var next        = ' <a class="next" href="{url}">下一页</a>';
var pages       = ' <span class="pages">{items}</span>';
var pageLink    = ' <a class="page" href="{url}">{number}</a>';
var currentPage = ' <span class="current">{number}</span>';
var total       = '<span class="total">共{number}页</span>';
var template    = '<span class="pager">{total}{first}{prev}{pages}{next}{last}</span>';


/**
* 输出分页导航 HTML
* 
*/
function draw(current, total, pageSize, callback) {
    return getHtml(current, total, pageSize, callback);
}

/**
* 得到分页导航 HTML
* 
*/
function getHtml(current, total, pageSize, callback) {
       if(total % pageSize == 0)
        var totalPage = total / pageSize;
       else
        var totalPage = parseInt(total / pageSize) + 1;
       $ret = this.template;
       $ret = $ret.replace("{total}", getTotal(totalPage));
       $ret = $ret.replace("{first}", getFirst(current, callback));
       $ret = $ret.replace("{prev}", getPrev(current, callback));
       $ret = $ret.replace("{pages}", getPages(current, totalPage, callback));
       $ret = $ret.replace("{next}", getNext(current, totalPage, callback));
       $ret = $ret.replace("{last}", getLast(current, totalPage, callback));
       return $ret;
}

function getPages(current, totalPage, callback) {
    if(this.itemCount % 2 == 0)
        var center = this.itemCount / 2;
       else
        var center = parseInt(this.itemCount / 2) + 1;
    
    var begin = current - center;
    
    var end = current + this.itemCount - center - 1;
    if (begin < 1) {
        begin = 1;
        end = this.itemCount;
    }
    if (end > totalPage) {
        end = totalPage;
        if((totalPage - this.itemCount)>0)
            begin = totalPage - this.itemCount + 1;
        else
            begin = 1;
    }
    var _pages = '';
    for (var i = begin; i <= end; i++) {
        var _pageLink = pageLink;
        if (i != current) {
            _pageLink = _pageLink.replace("{number}", i);
            _pageLink = _pageLink.replace("{url}", callback.replace("{p}", i));
            _pages += _pageLink;
        } else {
            _pageLink = " <span>"+i+"</span>";
            _pageLink = _pageLink.replace("{number}", i);
            _pages += _pageLink;
        }
    }
    return  this.pages.replace("{items}", _pages);
}
/**
* 第一页
* 
* @return string
*/
function getFirst(current, callback)
{
   if (current > 1) {
        return this.first.replace("{url}", callback.replace("{p}", 1));
    }
       return '';
}

/**
* 最后一页
* 
* @return string
*/
function getLast(current, total, callback)
{
   if (current < total) {
        return this.last.replace("{url}", callback.replace("{p}", total));
       }
       return '';
}

/**
* 上一页
* 
* @return string
*/
function getPrev(current, callback)
{
       if (current > 1) {
            return this.prev.replace("{url}", callback.replace("{p}", parseInt(current)-1));
       }
       return '';
}

/**
* 下一页
* 
* @return string
*/
function getNext(current, total, callback)
{
       if (current < total) {
            return this.next.replace("{url}", callback.replace("{p}", parseInt(current)+1));
       }
       return '';
}

/**
* 总页数
* 
* @return string
*/
function getTotal(total)
{
    
    return this.total.replace("{number}", total);
}
